package com.example.demo.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.core.handler.exception.ResultBO;
import com.example.demo.core.handler.exception.ResultTool;
import com.example.demo.sys.service.PermissionService;
import com.example.demo.sys.service.UserService;
import com.example.demo.sys.service.bo.RoleBO;
import com.example.demo.sys.service.bo.UserFullBO;
import com.example.demo.sys.service.dto.RoleConfigDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/management")
public class ManagementController {
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/users")
    @ResponseBody
    public ResultBO<?> getUsers() {
        List<UserFullBO> users = userService.getAllUsers();
        return ResultTool.success(users);
    }

    @DeleteMapping("/user/{ids}")
    @ResponseBody
    public ResultBO<?> removeUsers(@PathVariable("ids") List<String> ids) {
        userService.removeUsers(ids);
        return ResultTool.success(ids);
    }

    @GetMapping("/roles")
    @ResponseBody
    public ResultBO<?> getRoles() {
        List<RoleBO> roles = permissionService.getRoles();
        return ResultTool.success(roles);
    }

    @PutMapping("/role_config")
    @ResponseBody
    public ResultBO<?> updateRoleConfig(@RequestBody RoleConfigDTO rc) {
        JSONObject object = new JSONObject();
        permissionService.roleConfig(rc);
        return ResultTool.success(object);
    }
}
